<template>
  <Modal
    v-model="showCreateTestsuiteSetModal"
    title="创建场景集"
    :mask-closable="false"
    @on-cancel="handleCreateTestsuiteSetCancel"
  >
    <Form
      label-position="right"
      :label-width="100"
      ref="testsuiteSetForm"
      :model="testsuiteSetForm"
      :rules="testsuiteSetValidate"
    >
      <FormItem label="场景集名称" prop="name">
        <Input clearable v-model="testsuiteSetForm.name" />
      </FormItem>
      <FormItem label="所属项目" prop="project">
        <Select v-model="testsuiteSetForm.project" clearable filterable>
          <Option v-for="item in projects" :value="item.id" :key="item.id">{{
            item.name
          }}</Option>
        </Select>
      </FormItem>
      <FormItem label="备注">
        <Input
          type="textarea"
          clearable
          v-model="testsuiteSetForm.remark"
          :rows="3"
        />
      </FormItem>
    </Form>
    <div slot="footer">
      <Button type="default" @click="handleCreateTestsuiteSetCancel"
        >取消</Button
      >
      <Button
        type="primary"
        @click="handleCreateTestsuiteSubmit('testsuiteSetForm')"
        >确定</Button
      >
    </div>
  </Modal>
</template>

<script>
import { createTestsuiteSet } from "@/api/HttpAutoTestService/testsuite-set";
import { getProjectNames } from "@/api/HttpAutoTestService/project";

export default {
  name: "testsuite-set-create",
  props: {
    createTestsuiteSet: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    showCreateTestsuiteSetModal: {
      get() {
        return this.createTestsuiteSet;
      },
      set(val) {
        this.$emit("update:createTestsuiteSet", val);
      },
    },
  },
  data() {
    return {
      projects: [],
      testsuiteSetForm: {
        name: "",
        project: "",
        remark: "",
      },
      testsuiteSetValidate: {
        name: [
          { required: true, message: "请输入场景集名称", trigger: "blur" },
        ],
        project: [
          { required: true, message: "请选择所属项目", trigger: "change" },
        ],
      },
    };
  },
  watch: {
    createTestsuiteSet(val) {
      if (val) {
        getProjectNames()
          .then((res) => {
            this.projects = res.data;
          })
          .catch((err) => {
            this.$Modal.error({
              title: "系统异常，请联系管理员……",
              content: JSON.stringify(err),
              closable: true,
            });
          });
      }
    },
  },
  methods: {
    handleCreateTestsuiteSetCancel() {
      this.showCreateTestsuiteSetModal = false;
      this.testsuiteSetForm = {
        name: "",
        project: "",
        remark: "",
      };
      this.$emit("initTestsuiteSetTable");
      this.$refs['testsuiteSetForm'].resetFields();
    },
    handleCreateTestsuiteSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          let testsuiteSetForm = { ...this.testsuiteSetForm };

          createTestsuiteSet(testsuiteSetForm)
            .then((res) => {
              if (res.status === 201) {
                this.$Message.info("创建成功");
                this.handleCreateTestsuiteSetCancel();
              } else {
                this.$Message.error("创建失败");
              }
            })
            .catch((err) => {
              this.$Modal.error({
                title: "系统异常，请联系管理员……",
                content: JSON.stringify(err),
                closable: true,
              });
            });
        }
      });
    },
  },
};
</script>

<style>
</style>

